#include "task_sensorData.h"
#include "tasksInit.h"

#include <stdio.h>

#include "los_task.h"
#include "los_event.h"
#include "los_tick.h"
#include "los_mux.h"

#include "bsp_e53.h"
#include "bsp_mq2.h"

unsigned int task_sensorData_id;
TSK_INIT_PARAM_S task_sensorData_handler = {
    (TSK_ENTRY_FUNC)task_sensorData,
    27,
    0,
    1280,
    "task_sensorData",
    0
};

sensor_data_t sensor_data;
uint32_t sensor_data_mux;

void task_sensorData(void)
{
    uint32_t ret = 0;

    LOS_EventRead(&bspInit_ECB, BSPINIT_EVENT_INITOK, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);

    e53_data_t e53_data;

    float mq2_ppm = 0;
    // mq2_calibrate_R0(); //预先校准R0
    printf("[task_sensorData] calibrated R0: %f.\n", mq2_calibrate_R0());

    while(1)
    {
        // printf("This is task_sensorData. \n");

        e53_ia_read_data(&e53_data);

        //获取信号量
        LOS_MuxPend(sensor_data_mux, LOS_WAIT_FOREVER);
        sensor_data.lumi_lux = e53_data.luminance;
        sensor_data.humidity = e53_data.humidity;
        sensor_data.tempe_c = e53_data.temperature;

        sensor_data.mq2_ppm = mq2_get_ppm_tow();

        sensor_data.updated_timeTick = LOS_TickCountGet();
        LOS_MuxPost(sensor_data_mux);

        ret = LOS_QueueWriteCopy(sensorData_qId, &sensor_data, sizeof(sensor_data), 1);
        // if(ret != LOS_OK)
        // {
        //     printf("LOS_QueueWriteCopy error, ret=0x%x.\n", ret);
        // }

        LOS_Msleep(1000);

    }
    
}
